package com.mai4j.maiadmin.controller.api.system.localisation;

import com.mai4j.maiadmin.misc.StructMapper;
import com.mai4j.maiadmin.model.RespCode;
import com.mai4j.maiadmin.model.dto.CurrencyDTO;
import com.mai4j.maiadmin.model.dto.CurrencyDescriptionDTO;
import com.mai4j.maiadmin.model.dto.PageDTO;
import com.mai4j.maiadmin.model.http.CurrencyDeleteReq;
import com.mai4j.maiadmin.model.http.CurrencyGetReq;
import com.mai4j.maiadmin.model.http.CurrencySaveOrUpdateReq;
import com.mai4j.maiadmin.model.http.datatables.Datatable;
import com.mai4j.maiadmin.model.http.datatables.response.DatatableResp;
import com.mai4j.maiadmin.model.http.response.CurrencyGetResp;
import com.mai4j.maiadmin.model.http.response.CurrencyListResp;
import com.mai4j.maiadmin.model.http.response.CurrencySaveOrUpdateResp;
import com.mai4j.maiadmin.model.http.response.SimpleResp;
import com.mai4j.maiadmin.service.CurrencyService;
import com.mai4j.maiadmin.service.ExchangeRateService;
import com.mai4j.maiadmin.support.I18nProvider;
import com.mai4j.maiadmin.support.i18n.I18n;
import com.mai4j.maiadmin.support.security.aop.annotation.RequiresAuthentication;
import com.mai4j.maiadmin.support.web.binding.ReqParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 微信公众号请关注: 开源螺丝钉
 * <br>
 * 码云请关注: <a href="https://gitee.com/xiyoufang">https://gitee.com/xiyoufang</a>
 * <br>
 * 哔哩哔哩请关注: <a href="https://space.bilibili.com/438927834">https://space.bilibili.com/438927834</a>
 *
 * @author xiyoufang
 */
@RequiresAuthentication
@RestController
@RequestMapping("/api/system/localisation/currency")
public class ApiCurrencyController {

    private final CurrencyService currencyService;

    private final ExchangeRateService exchangeRateService;

    public ApiCurrencyController(CurrencyService currencyService, ExchangeRateService exchangeRateService) {
        this.currencyService = currencyService;
        this.exchangeRateService = exchangeRateService;
    }

    @PostMapping("/list")
    public ResponseEntity<CurrencyListResp> list() {
        List<CurrencyDTO> currencyDTOS = currencyService.list();
        return ResponseEntity.ok(new CurrencyListResp()
                .setCurrencies(currencyDTOS.stream()
                        .filter(c -> Objects.equals(c.getStatus(), (byte) 1))
                        .map(c -> new CurrencyListResp.Currency()
                                .setCode(c.getCode())
                                .setName(c.getDescriptions()
                                        .stream()
                                        .filter(d -> Objects.equals(d.getLanguageCode(), I18n.getTag()))
                                        .map(CurrencyDescriptionDTO::getName)
                                        .findFirst()
                                        .orElse("")))
                        .collect(Collectors.toList()))
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }

    @PostMapping("/get")
    public ResponseEntity<CurrencyGetResp> get(@RequestBody @Valid CurrencyGetReq currencyGetReq) {
        CurrencyDTO currencyDTO = currencyService.get(Long.parseLong(currencyGetReq.getCurrencyId()));
        return ResponseEntity.ok(new CurrencyGetResp()
                .setCurrency(StructMapper.INSTANCE.currencyDTOToCurrencyVO(currencyDTO))
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }

    @PostMapping("/refresh")
    public ResponseEntity<SimpleResp> refresh() {
        exchangeRateService.update();
        return ResponseEntity.ok(new SimpleResp()
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }

    /**
     * 分页
     *
     * @param datatable datatable
     */
    @PostMapping("/datatable")
    public ResponseEntity<DatatableResp> datatable(@ReqParam Datatable datatable) {
        PageDTO<CurrencyDTO> pageDTO = currencyService.paginate(datatable);
        return ResponseEntity.ok(new DatatableResp()
                .setDraw(datatable.getDraw())
                .setRecordsFiltered(pageDTO.getTotalRow())
                .setRecordsTotal(pageDTO.getTotalRow())
                .setData(pageDTO.getList()
                        .stream()
                        .map(StructMapper.INSTANCE::currencyDTOToCurrencyVO)
                        .collect(Collectors.toList())));
    }

    @PostMapping("/saveOrUpdate")
    public ResponseEntity<CurrencySaveOrUpdateResp> saveOrUpdate(@RequestBody @Valid CurrencySaveOrUpdateReq currencySaveOrUpdateReq) {
        CurrencyDTO currencyDTO = currencyService.saveOrUpdate(StructMapper.INSTANCE.currencySaveOrUpdateReqToCurrencyDTO(currencySaveOrUpdateReq));
        return ResponseEntity.ok(new CurrencySaveOrUpdateResp()
                .setCurrencyId(String.valueOf(currencyDTO.getCurrencyId()))
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get(
                        Objects.equals(String.valueOf(currencyDTO.getCurrencyId()), currencySaveOrUpdateReq.getCurrencyId())
                                ? "text.edit.successfully"
                                : "text.save.successfully")));
    }

    @PostMapping("/delete")
    public ResponseEntity<CurrencyGetResp> delete(@RequestBody @Valid CurrencyDeleteReq currencyGetReq) {
        currencyService.delete(Long.parseLong(currencyGetReq.getCurrencyId()));
        return ResponseEntity.ok(new CurrencyGetResp()
                .setCode(RespCode.OK.getCode())
                .setMessage(I18nProvider.get("text.successfully")));
    }
}
